package com.yisu.auction.api.web.utils;

import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.*;
import com.pingplusplus.model.Charge;
import com.yisu.auction.api.domain.utils.IpAddrUtil;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/8
 */
public class PingppUtil {

    /**
     *
     * @param orderNo
     * @param amount
     * @param payType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getChargeCommon(String orderNo,String amount,Integer payType,String callbackUrl,String appSign) throws UnsupportedEncodingException {
        if (!StringUtils.isEmpty(orderNo)) {
            Charge charge = null;
         /*   PingppChannelVo pingppChannelVo=new PingppChannelVo(appSign);
            //更改使用ping++支付
            Charge charge = null;
            Pingpp.apiKey = pingppChannelVo.getApiKey();
            java.net.URL uri = Thread.currentThread().getContextClassLoader().getResource("");
            String path = URLDecoder.decode(uri.getPath(), "UTF-8") + pingppChannelVo.getPathFileName();
            Pingpp.privateKeyPath = path;
            Map<String, Object> chargeParams = new HashMap<String, Object>();
            chargeParams.put("order_no", orderNo);
          *//*  chargeParams.put("amount", new BigDecimal(amount).multiply(BigDecimal.valueOf(100)));*//*
            chargeParams.put("amount", new BigDecimal("0.01").multiply(BigDecimal.valueOf(100)));
            Map<String, String> app = new HashMap<String, String>();
            app.put("id", pingppChannelVo.getAppId());*/

            Pingpp.apiKey = "sk_live_n1O400GmLev51GuvfTifrv5K";
            java.net.URL uri = Thread.currentThread().getContextClassLoader().getResource("");
            String path = URLDecoder.decode(uri.getPath(), "UTF-8") + "pingxx.pem";
            Pingpp.privateKeyPath = path;
            Map<String, Object> chargeParams = new HashMap<String, Object>();
            chargeParams.put("order_no", orderNo);
            chargeParams.put("amount", new BigDecimal(amount).multiply(BigDecimal.valueOf(100)));
           /* chargeParams.put("amount", new BigDecimal("0.01").multiply(BigDecimal.valueOf(100)));*/
            Map<String, String> app = new HashMap<String, String>();
            app.put("id", "app_PeTKW5yHCmzTz1W5");

            chargeParams.put("app", app);
            chargeParams.put("currency", "cny");
            chargeParams.put("client_ip", IpAddrUtil.GetIp());
            String subject = "充值";
            chargeParams.put("subject", subject);
            String desc = "充值";
            chargeParams.put("body", desc);
            chargeParams.put("description", "RobTreasureOrder");

            chargeParams.put("time_expire", DateTime.now().plusMinutes(10).getMillis() / 1000);
            if (payType == 1) {
                chargeParams.put("channel", "alipay");
            }
            if (payType == 2) {
                chargeParams.put("channel", "wx");
            }
            if (payType == 3) {
                Map<String, String> extra = new HashMap<String, String>();
                //success_url 和 cancel_url 在本地测试不要写 localhost ，写 127.0.0.1，URL 后面不要加自定义参数
                extra.put("success_url", "http://weixin.hzyisu.com/#/ding/paySuccess?callbackUrl=" + callbackUrl);
                extra.put("cancel_url", "http://weixin.hzyisu.com/#/ding/payFailure?callbackUrl=" + callbackUrl);
                chargeParams.put("extra", extra);
                chargeParams.put("channel", "alipay_wap");
            }
            try {
                //发起交易请求
                charge = Charge.create(chargeParams);
            } catch (APIConnectionException e) {
                e.printStackTrace();
            } catch (ChannelException e) {
                e.printStackTrace();
            } catch (RateLimitException e) {
                e.printStackTrace();
            } catch (AuthenticationException e) {
                e.printStackTrace();
            } catch (APIException e) {
                e.printStackTrace();
            } catch (InvalidRequestException e) {
                e.printStackTrace();
            }
            return charge.toString();
        }
        return "";
    }
}
